﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Booking_Hotel.BLL;
using Booking_Hotel.DBML;
using System.Web.Security;
using Booking_Hotel.DAL;

namespace Booking_Hotel
{
    public partial class supplier : System.Web.UI.Page
    {
        Supplier supplierF = new Supplier();
        int IDSupplier;
        ThanhBHotelDataContext TBK = new ThanhBHotelDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void bt_Insert_Click(object sender, EventArgs e)
        {
            try
            {
                Supplier Checkkey = TBK.sp_SupplierSelectByUserName(txt_UserName.Text.Trim()).SingleOrDefault();
                if (Checkkey == null)
                {
                    supplierF = new Supplier();
                    supplierF.UserName = this.txt_UserName.Text.Trim();
                    string pass_hash = FormsAuthentication.HashPasswordForStoringInConfigFile(txt_PassWord.Text.Trim(), "MD5");
                    supplierF.Password = pass_hash;
                    supplierF.TenKinhDoanh = this.txt_TenKinhDoanh.Text.Trim();
                    supplierF.TenDaiDien = this.txt_TenDaiDien.Text.Trim();
                    supplierF.DiaChi = this.txt_DiaChi.Text.Trim();
                    supplierF.Mobile = this.txt_Mobile.Text.Trim();
                    supplierF.DienThoaiBan = this.txt_DienThoaiBan.Text.Trim();
                    SupplierDAO sp = new SupplierDAO();
                    sp.addSupplier(supplierF);
                    gv_Supplier.DataBind();
                }
                else
                {
                    MyInclude.MessageBox.ShowMessage("Supplier: " + txt_UserName.Text + ", đã tồn tại vui lòng đổi một username khác.", this.Page);
            
                }

            }
            catch (Exception)
            {
                
                throw;
            }
           
        }

        protected void bt_Update_Click(object sender, EventArgs e)
        {
            try
            {
                supplierF = TBK.sp_SupplierSelectByUserName(txt_UserName.Text.Trim()).SingleOrDefault();//(01)
                if (supplierF != null)
                {
                    int SupplierID = supplierF.SupplierID; //Lấy từ supplierF (01)
                    supplierF = new Supplier();            // Set mới lại.
                    supplierF.SupplierID = SupplierID;
                    string pass_hash = FormsAuthentication.HashPasswordForStoringInConfigFile(txt_PassWord.Text.Trim(), "MD5");
                    supplierF.Password = pass_hash;
                    supplierF.TenKinhDoanh = txt_TenKinhDoanh.Text.Trim();
                    supplierF.TenDaiDien = txt_TenDaiDien.Text.Trim();
                    supplierF.DiaChi = txt_DiaChi.Text.Trim();
                    supplierF.Mobile = txt_Mobile.Text.Trim();
                    supplierF.DienThoaiBan = txt_DienThoaiBan.Text.Trim();
                    SupplierDAO sp = new SupplierDAO();
                    sp.updateSupplier(supplierF);
                    gv_Supplier.DataBind();
                }
                else
                {
                    MyInclude.MessageBox.ShowMessage("Supplier: " + txt_UserName.Text +", không tồn tại để thực hiện update." , this.Page);
                }

            }
            catch (Exception)
            {
                
                throw;
            }
            

        }



        protected void gv_Supplier_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                //Đã có thông báo xáo bên code giao diện.
                IDSupplier = Convert.ToInt32(gv_Supplier.Rows[e.RowIndex].Cells[0].Text.Trim());
                supplierF = SupplierServices.GetSupplierByID(IDSupplier);
    
            }
            catch (Exception)
            {
                
                throw;
            }

           
        }

        protected void gv_Supplier_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
           
            GridViewRow select = gv_Supplier.Rows[e.NewSelectedIndex];
            txt_UserName.Text = select.Cells[1].Text.Trim();
            txt_TenKinhDoanh.Text = select.Cells[2].Text.Trim();
            txt_TenDaiDien.Text = select.Cells[3].Text.Trim();
            txt_DiaChi.Text = select.Cells[4].Text.Trim();
            txt_Mobile.Text = select.Cells[5].Text.Trim();
            txt_DienThoaiBan.Text = select.Cells[6].Text.Trim();
    
         }

        protected void gv_Supplier_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 0)
            {
                e.Row.Cells[0].Visible = false;
            }
        }   
    }
}